package com.longlong.controller.frontDeskApi;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longlong.entity.WxGzhUser;
import com.longlong.mapper.WxGzhUserMapper;
import com.longlong.service.WxGzhUserService;
import com.longlong.utils.WxLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/font")
public class wx_gzh {

    @Autowired
    private WxGzhUserService wxGzhUserService;

    @Autowired
    private WxLoginUtil wxLoginUtil;

    @Autowired
    private WxGzhUserMapper wxGzhUserMapper;



    /*
    * 获取code
    * */
    @RequestMapping("/getCode")
    public String getCode(@RequestParam("redirectUrl") String redirectUrl){
        String code = wxGzhUserService.getCode(redirectUrl);
        return code;
    }



    /*
    * 获取openid
    * */
    @RequestMapping("/getOpenid")
    private Object getOpenid(@RequestParam String code){
        //获取到用户的openid
        String openid = wxLoginUtil.getOpenid(code);
        //判断用户的openid是否存在与数据库
        QueryWrapper<WxGzhUser> wrapper = new QueryWrapper<>();
        wrapper.eq("openid_gzh",openid);
        WxGzhUser wxGzhUser = new WxGzhUser();
        if(wxGzhUserMapper.selectOne(wrapper)==null){
            //如果用户的openid不存在  则再数据库中存储这个用户的openid
            wxGzhUser.setOpenidGzh(openid);
            wxGzhUserMapper.insert(wxGzhUser);
        }
        //如果openid存在就直接返回给用户openid
        return openid;
    }


}
